//
//  IOS_SDKViewControllerRasterMode.m
//  IOS_SDK
//
//  Created by Tzvi on 8/2/11.
//  Copyright 2011 STAR MICRONICS CO., LTD. All rights reserved.
//

#import "AppDelegate.h"

#import "IOS_SDKViewControllerRasterMode.h"
#import "StarIO/SMPort.h"
#import "PrinterFunctions.h"
#import "Cut.h"
#import "rasterPrinting.h"
#import "StandardHelp.h"

#import <QuartzCore/QuartzCore.h>

@implementation IOS_SDKViewControllerRasterMode

- (id)init
{
    self = [super init];
    if (self)
    {
        
    }
    return self;
}

- (IBAction)pushButtonBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        arrayPortNumber = [[NSMutableArray alloc] init];
        [arrayPortNumber addObject:@"Standard"];
        [arrayPortNumber addObject:@"9100"];
        [arrayPortNumber addObject:@"9101"];
        [arrayPortNumber addObject:@"9102"];
        [arrayPortNumber addObject:@"9103"];
        [arrayPortNumber addObject:@"9104"];
        [arrayPortNumber addObject:@"9105"];
        [arrayPortNumber addObject:@"9106"];
        [arrayPortNumber addObject:@"9107"];
        [arrayPortNumber addObject:@"9108"];
        [arrayPortNumber addObject:@"9109"];

        arrayFunction = [[NSMutableArray alloc] init];
        [arrayFunction addObject:@"Get Status"];
        [arrayFunction addObject:@"Sample Receipt"];
        [arrayFunction addObject:@"JP Sample Receipt"];
        [arrayFunction addObject:@"Open Cash Drawer"];
        [arrayFunction addObject:@"Raster Graphics Text Printing"];
    }
    return self;
}

- (void)dealloc
{
    [arrayPortNumber release];
    [arrayFunction   release];

    [buttonBack release];
    [tableviewPortNumber release];
    [tableviewFunction release];
    [super dealloc];
}

+ (void)setPortName:(NSString *)m_portName
{
    [AppDelegate setPortName:m_portName];
}

+ (void)setPortSettings:(NSString *)m_portSettings
{
    [AppDelegate setPortSettings:m_portSettings];
}

- (void)SetPortInfo
{
    NSString *localPortName = [NSString stringWithString: uitextfield_portname.text];
    [IOS_SDKViewControllerRasterMode setPortName:localPortName];
    
    NSString *localPortSettings = @"";

    switch ([pickerpopupPortNumber getSelectedIndex])
    {
        default:                              break;
        case 1 : localPortSettings = @"9100"; break;
        case 2 : localPortSettings = @"9101"; break;
        case 3 : localPortSettings = @"9102"; break;
        case 4 : localPortSettings = @"9103"; break;
        case 5 : localPortSettings = @"9104"; break;
        case 6 : localPortSettings = @"9105"; break;
        case 7 : localPortSettings = @"9106"; break;
        case 8 : localPortSettings = @"9107"; break;
        case 9 : localPortSettings = @"9108"; break;
        case 10: localPortSettings = @"9109"; break;
    }

    [IOS_SDKViewControllerRasterMode setPortSettings:localPortSettings];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [uitextfield_portname setDelegate:self];
    
    pickerpopupPortNumber = [[PickerPopup alloc] init];
    [pickerpopupPortNumber setListener:@selector(DismissActionSheet:): self];
    [pickerpopupPortNumber setDataSource:arrayPortNumber];
    
    [tableviewPortNumber setDelegate:pickerpopupPortNumber];
    [tableviewPortNumber setDataSource:pickerpopupPortNumber];
    tableviewPortNumber.layer.borderWidth = 1;

    tableviewFunction.dataSource = self;
    tableviewFunction.delegate   = self;
}

- (void)viewDidUnload
{
    [pickerpopupPortNumber release];
    [buttonBack release];
    buttonBack = nil;
    [tableviewPortNumber release];
    tableviewPortNumber = nil;
    [tableviewFunction release];
    tableviewFunction = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arrayFunction count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    int index = indexPath.row;
    [cell.textLabel setText: [arrayFunction objectAtIndex:index]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSString *portName     = @"";
    NSString *portSettings = @"";

    selectIndex = indexPath.row;

    switch (selectIndex)
    {
        case 0:   
            [uitextfield_portname resignFirstResponder];
        
            [self SetPortInfo];
        
            portName     = [AppDelegate getPortName];
            portSettings = [AppDelegate getPortSettings];
        
            [PrinterFunctions CheckStatusWithPortname:portName portSettings:portSettings];
            break;
            
        case 1:
        case 2:
            {
                UIActionSheet *actionsheetSampleReceipt = [[UIActionSheet alloc] initWithTitle:@"Paper Width" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"3 inch" otherButtonTitles:@"4 inch",nil];
                [actionsheetSampleReceipt showInView:self.view];
                [actionsheetSampleReceipt release];
            }
            
            break;
        case 3:
            [uitextfield_portname resignFirstResponder];
        
            [self SetPortInfo];
        
            portName     = [AppDelegate getPortName];
            portSettings = [AppDelegate getPortSettings];
        
            [PrinterFunctions OpenCashDrawerWithPortname:portName portSettings:portSettings];
            break;
        case 4:
            [uitextfield_portname resignFirstResponder];
        
            [self SetPortInfo];
        
            rasterPrinting *rasterPrintingVar = [[rasterPrinting alloc] initWithNibName:@"rasterPrinting" bundle:[NSBundle mainBundle]];
            [self.navigationController pushViewController:rasterPrintingVar animated:YES];
            [rasterPrintingVar release];
            break;
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *portName     = @"";
    NSString *portSettings = @"";
    
    switch (buttonIndex)
    {
        case 0:
            [uitextfield_portname resignFirstResponder];
            
            [self SetPortInfo];
            
            portName     = [AppDelegate getPortName];
            portSettings = [AppDelegate getPortSettings];
            
            switch (selectIndex)
            {
                case 1: [PrinterFunctions PrintRasterSampleReceipt3InchWithPortname     :portName portSettings:portSettings]; break;
                case 2: [PrinterFunctions PrintRasterKanjiSampleReceipt3InchWithPortname:portName portSettings:portSettings]; break;
            }
            
            break;
            
        case 1:
            [uitextfield_portname resignFirstResponder];
            
            [self SetPortInfo];
            
            portName     = [AppDelegate getPortName];
            portSettings = [AppDelegate getPortSettings];
            
            switch (selectIndex)
            {
                case 1: [PrinterFunctions PrintRasterSampleReceipt4InchWithPortname     :portName portSettings:portSettings]; break;
                case 2: [PrinterFunctions PrintRasterKanjiSampleReceipt4InchWithPortname:portName portSettings:portSettings]; break;
            }
            
            break;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)DismissActionSheet:(id)unusedID
{
    [tableviewPortNumber reloadData];
    [tableviewFunction reloadData];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqualToString:@"\n"] == true)
    {
        [uitextfield_portname resignFirstResponder];
        return NO;
    }
    return YES;
}

- (IBAction)pushButtonSearch:(id)sender
{
    searchView = [[SearchPrinterViewController alloc] initWithNibName:@"SearchPrinterViewController" bundle:nil];
    
    [self.navigationController pushViewController:searchView animated:YES];
    
    [searchView release];
}

- (IBAction)showHelp
{
    NSString *title = @"PORT PARAMETERS";
    
    NSString *helpText = [AppDelegate HTMLCSS];
    helpText = [helpText stringByAppendingString:@"<body>\
                 This program on supports ethernet cards<br/>\
                 <Code>TCP:192.168.222.244</Code><br/>\
                 <It1>Enter IP address of Star Printer</It1><br/><br/>\
                 <LargeTitle><center>Port Settings</center></LargeTitle>\
                 <p>You should leave this blank for desktop printers.</p>\
                 </body><html>"];
    
    StandardHelp *helpVar = [[StandardHelp alloc]initWithNibName:@"StandardHelp" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:helpVar animated:YES];
    [helpVar release];

    [helpVar setHelpTitle:title];
    [helpVar setHelpText:helpText];
}

- (void)returnSelectedCellText {
    
    NSString *selectedPortName = [searchView lastSelectedPortName];
    
    if ((selectedPortName != nil) && (selectedPortName != @""))
    {
        uitextfield_portname.text = selectedPortName;
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
